<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/xlrblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/xlrblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/xlrblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/xlrblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/xlrblog/css/main.css">


<link rel="stylesheet" href="/xlrblog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/xlrblog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="本文涉及3种类型的后端接口，Body型、Param型和File型发送请求的方法  接口测试UserService.java: 12345678910111213141516171819202122232425262728293031323334353637383940414243@org.springframework.stereotype.Service(&quot;UserService&amp;qu">
<meta property="og:type" content="article">
<meta property="og:title" content="Android前后端交互">
<meta property="og:url" content="https://gitee.com/xlr0306/2022/11/01/Android%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/index.html">
<meta property="og:site_name" content="君不见的博客">
<meta property="og:description" content="本文涉及3种类型的后端接口，Body型、Param型和File型发送请求的方法  接口测试UserService.java: 12345678910111213141516171819202122232425262728293031323334353637383940414243@org.springframework.stereotype.Service(&quot;UserService&amp;qu">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBb6f66c892ba59ddd3d6e3d2037a2ba95?method=download&shareKey=35d2a752187b4c3737ff08c0a34e2c8c">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEB34c8d05c26c39ee88f45e516275c2038?method=download&shareKey=f82e28e53dff23233fbb75b989ce2d87">
<meta property="og:image" content="https://note.youdao.com/yws/api/personal/file/WEBf1ce261a38255939b43d1707e11cd301?method=download&shareKey=089e67df5a56d2db43e323d6d7b902c6">
<meta property="article:published_time" content="2022-11-01T06:21:00.000Z">
<meta property="article:modified_time" content="2022-11-01T06:33:40.192Z">
<meta property="article:author" content="君不见">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Note">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://note.youdao.com/yws/api/personal/file/WEBb6f66c892ba59ddd3d6e3d2037a2ba95?method=download&shareKey=35d2a752187b4c3737ff08c0a34e2c8c">

<link rel="canonical" href="https://gitee.com/xlr0306/2022/11/01/Android%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Android前后端交互 | 君不见的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/xlrblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">君不见的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录生活中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/xlrblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/xlrblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/xlrblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/xlrblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/xlr0306/2022/11/01/Android%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/xlrblog/images/avatar.gif">
      <meta itemprop="name" content="君不见">
      <meta itemprop="description" content="君不见，黄河之水天上来，奔流到海不复回。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="君不见的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Android前后端交互
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-11-01 14:21:00 / 修改时间：14:33:40" itemprop="dateCreated datePublished" datetime="2022-11-01T14:21:00+08:00">2022-11-01</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/xlrblog/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本文涉及3种类型的后端接口，Body型、Param型和File型发送请求的方法 </p>
<h2 id="接口测试"><a href="#接口测试" class="headerlink" title="接口测试"></a>接口测试</h2><p>UserService.java:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@org</span>.springframework.stereotype.Service(<span class="string">&quot;UserService&quot;</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserService</span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> List&lt;UserEntity&gt; <span class="title function_">queryLimit</span><span class="params">(Integer currentPage,Integer pageSzie)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.queryLimit(currentPage,pageSzie);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">addUser</span><span class="params">(UserEntity user)</span>&#123;</span><br><span class="line">        userDao.addUser(user);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">updateUser</span><span class="params">(UserEntity user)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.updateUser(user);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">deleteUser</span><span class="params">(UserEntity user)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.deleteUser(user);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Resp&lt;String&gt; <span class="title function_">upload</span><span class="params">(MultipartFile file)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(file.isEmpty()) &#123;</span><br><span class="line">            <span class="keyword">return</span> Resp.fail(<span class="string">&quot;400&quot;</span>,<span class="string">&quot;文件为空！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">String</span> <span class="variable">originalFileName</span> <span class="operator">=</span> file.getOriginalFilename();</span><br><span class="line">        <span class="type">String</span> <span class="variable">fileName</span> <span class="operator">=</span>   System.currentTimeMillis()+<span class="string">&quot;.&quot;</span>+originalFileName</span><br><span class="line">                .substring(originalFileName.lastIndexOf(<span class="string">&#x27;.&#x27;</span>)+<span class="number">1</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">filePath</span> <span class="operator">=</span> <span class="string">&quot;D:\\myTemp\\&quot;</span>;</span><br><span class="line">        <span class="type">File</span> <span class="variable">dest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(filePath+fileName);</span><br><span class="line">        <span class="keyword">if</span>(!dest.getParentFile().exists()) &#123;</span><br><span class="line">            dest.getParentFile().mkdirs();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            file.transferTo(dest);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            <span class="keyword">return</span> Resp.fail(<span class="string">&quot;500&quot;</span>,originalFileName+<span class="string">&quot;上传失败！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Resp.success(fileName);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>UserController.java：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;user&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">    <span class="type">String</span> <span class="variable">message</span> <span class="operator">=</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/queryLimit&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> List&lt;UserEntity&gt; <span class="title function_">queryLimit</span><span class="params">(<span class="meta">@RequestParam(&quot;currentPage&quot;)</span> Integer currentPage,<span class="meta">@RequestParam(&quot;pageSize&quot;)</span> Integer pageSize)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userService.queryLimit(currentPage,pageSize);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping(&quot;/addUser&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">addUer</span><span class="params">(<span class="meta">@RequestBody</span> UserEntity user)</span>&#123;</span><br><span class="line">        <span class="comment">//用Mybatis执行insert语句的时候，插入成功会返回1，不成功则会抛出异常，捕获一下异常就好</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            userService.addUser(user);</span><br><span class="line">            message = <span class="string">&quot;增加用户成功&quot;</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception exception)&#123;</span><br><span class="line">            message = <span class="string">&quot;增加用户异常&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> message;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PutMapping(&quot;/updateUser&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">updateUser</span><span class="params">(<span class="meta">@RequestBody</span> UserEntity user)</span>&#123;</span><br><span class="line">        <span class="comment">//Mybatis的更新操作成功返回1，用户不存在返回0，失败则抛异常</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            message = userService.updateUser(user) == <span class="number">1</span>?<span class="string">&quot;更新用户成功&quot;</span>:<span class="string">&quot;用户不存在，更新失败&quot;</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception exception)&#123;</span><br><span class="line">            message = <span class="string">&quot;更新异常&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> message;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@DeleteMapping(&quot;/deleteUser&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">deleteUser</span><span class="params">(<span class="meta">@RequestBody</span> UserEntity user)</span>&#123;</span><br><span class="line">        <span class="comment">//Mybatis的删除操作和更新返回值一样，成功返回1，用户不存在返回0，失败则抛异常</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            message = userService.deleteUser(user) == <span class="number">1</span>?<span class="string">&quot;删除用户成功&quot;</span>:<span class="string">&quot;用户不存在，删除失败&quot;</span>;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (Exception exception)&#123;</span><br><span class="line">            message = <span class="string">&quot;删除异常&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> message;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(value = &quot;/upload&quot;,method = RequestMethod.POST)</span></span><br><span class="line">    <span class="keyword">private</span> Resp&lt;String&gt; <span class="title function_">upload</span><span class="params">(<span class="meta">@RequestParam(&quot;file&quot;)</span>MultipartFile file)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> userService.upload(file);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Body类型"><a href="#Body类型" class="headerlink" title="Body类型"></a>Body类型</h3><p>Body类型的接口是类似于这种：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping(&quot;/addUser&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">addUer</span><span class="params">(<span class="meta">@RequestBody</span> UserEntity user)</span>&#123;</span><br><span class="line">    <span class="comment">//用Mybatis执行insert语句的时候，插入成功会返回1，不成功则会抛出异常，捕获一下异常就好</span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        userService.addUser(user);</span><br><span class="line">        message = <span class="string">&quot;增加用户成功&quot;</span>;</span><br><span class="line">    &#125;<span class="keyword">catch</span> (Exception exception)&#123;</span><br><span class="line">        message = <span class="string">&quot;增加用户异常&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> message;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>地址为：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/user/addUser">http://localhost:8080/user/addUser</a> </p>
</blockquote>
<p><img src=" https://note.youdao.com/yws/api/personal/file/WEBb6f66c892ba59ddd3d6e3d2037a2ba95?method=download&shareKey=35d2a752187b4c3737ff08c0a34e2c8c" ></p>

<h3 id="Param类型"><a href="#Param类型" class="headerlink" title="Param类型"></a>Param类型</h3><p>Param类型的接口是类似于这种：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/queryLimit&quot;)</span></span><br><span class="line"><span class="keyword">public</span> List&lt;UserEntity&gt; <span class="title function_">queryLimit</span><span class="params">(<span class="meta">@RequestParam(&quot;currentPage&quot;)</span> Integer currentPage,<span class="meta">@RequestParam(&quot;pageSize&quot;)</span> Integer pageSize)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userService.queryLimit(currentPage,pageSize);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>地址为：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/user/queryLimit">http://localhost:8080/user/queryLimit</a> </p>
</blockquote>
<p><img src="https://note.youdao.com/yws/api/personal/file/WEB34c8d05c26c39ee88f45e516275c2038?method=download&shareKey=f82e28e53dff23233fbb75b989ce2d87" alt="1667283558631"></p>
<h3 id="File类型"><a href="#File类型" class="headerlink" title="File类型"></a>File类型</h3><p>File类型的接口是类似于这种：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = &quot;/upload&quot;,method = RequestMethod.POST)</span></span><br><span class="line"><span class="keyword">private</span> Resp&lt;String&gt; <span class="title function_">upload</span><span class="params">(<span class="meta">@RequestParam(&quot;file&quot;)</span>MultipartFile file)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> userService.upload(file);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>地址为：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/user/upload">http://localhost:8080/user/upload</a> </p>
</blockquote>
<p><img src="https://note.youdao.com/yws/api/personal/file/WEBf1ce261a38255939b43d1707e11cd301?method=download&shareKey=089e67df5a56d2db43e323d6d7b902c6" alt="1667266319843"></p>
<h2 id="前后端连接"><a href="#前后端连接" class="headerlink" title="前后端连接"></a>前后端连接</h2><blockquote>
<p>首先要创建一个Thread，网络请求操作较慢，所以要在子线程里操作，在主线程里发送网络请求会造成堵塞，APP可能会崩溃。 其次是网络请求的操作要放在try语句里，毕竟网络通信不一定成功的。最后需要注意的是子线程不能直接更新UI，要在子线程里操作UI，需要借助runOnUiThread 。</p>
</blockquote>
<p>添加OkHttp的依赖：</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">implementation(<span class="string">&quot;com.squareup.okhttp3:okhttp:4.10.0&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>声明权限：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.INTERNET&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.READ_EXTERNAL_STORAGE&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="Body类型-1"><a href="#Body类型-1" class="headerlink" title="Body类型"></a>Body类型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Button</span> <span class="variable">add</span> <span class="operator">=</span> findViewById(R.id.add);</span><br><span class="line">add.setOnClickListener(<span class="keyword">new</span> <span class="title class_">View</span>.OnClickListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">(View view)</span> &#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="type">String</span> <span class="variable">json</span> <span class="operator">=</span> <span class="string">&quot;&#123;\n&quot;</span> +</span><br><span class="line">                            <span class="string">&quot;    \&quot;userId\&quot;: 8,\n&quot;</span> +</span><br><span class="line">                            <span class="string">&quot;    \&quot;userName\&quot;: \&quot;Y\&quot;,\n&quot;</span> +</span><br><span class="line">                            <span class="string">&quot;    \&quot;userAddress\&quot;: \&quot;Unknow\&quot;\n&quot;</span> +</span><br><span class="line">                            <span class="string">&quot;&#125;&quot;</span>;</span><br><span class="line">                    <span class="type">OkHttpClient</span> <span class="variable">client</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OkHttpClient</span>();<span class="comment">//创建http客户端</span></span><br><span class="line">                    <span class="type">Request</span> <span class="variable">request</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Request</span>.Builder()</span><br><span class="line">                            .url(<span class="string">&quot;http://172.17.152.37:8080/user/addUser&quot;</span>)</span><br><span class="line">                            .post(RequestBody.create(MediaType.parse(<span class="string">&quot;application/json&quot;</span>),json))</span><br><span class="line">                            .build();<span class="comment">//创建http请求</span></span><br><span class="line">                    <span class="type">Response</span> <span class="variable">response</span> <span class="operator">=</span> client.newCall(request).execute();<span class="comment">//执行发送的指令</span></span><br><span class="line">                    runOnUiThread(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                            Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;Add Success&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                    runOnUiThread(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                        <span class="meta">@Override</span></span><br><span class="line">                        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                            Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;Add Error&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;).start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h3 id="Param类型-1"><a href="#Param类型-1" class="headerlink" title="Param类型"></a>Param类型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Button</span> <span class="variable">query</span> <span class="operator">=</span> findViewById(R.id.query);</span><br><span class="line">query.setOnClickListener(<span class="keyword">new</span> <span class="title class_">View</span>.OnClickListener() &#123;</span><br><span class="line">           <span class="meta">@Override</span></span><br><span class="line">           <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">(View view)</span> &#123;</span><br><span class="line">               <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                   <span class="meta">@Override</span></span><br><span class="line">                   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                       <span class="keyword">try</span> &#123;</span><br><span class="line">                           FormBody.<span class="type">Builder</span> <span class="variable">params</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FormBody</span>.Builder();</span><br><span class="line">                           params.add(<span class="string">&quot;currentPage&quot;</span>,<span class="string">&quot;0&quot;</span>);</span><br><span class="line">                           params.add(<span class="string">&quot;pageSize&quot;</span>,<span class="string">&quot;5&quot;</span>);</span><br><span class="line">                           <span class="type">OkHttpClient</span> <span class="variable">client</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OkHttpClient</span>();<span class="comment">//创建http客户端</span></span><br><span class="line">                           <span class="type">Request</span> <span class="variable">request</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Request</span>.Builder()</span><br><span class="line">                                   .url(<span class="string">&quot;http://172.17.152.37:8080/user/queryLimit&quot;</span>)</span><br><span class="line">                                   .post(params.build())</span><br><span class="line">                                   .build();<span class="comment">//创建http请求</span></span><br><span class="line">                           <span class="type">Response</span> <span class="variable">response</span> <span class="operator">=</span> client.newCall(request).execute();<span class="comment">//执行发送的指令</span></span><br><span class="line">                           <span class="type">String</span> <span class="variable">responseData</span> <span class="operator">=</span> response.body().string();<span class="comment">//获取返回的JSON结果</span></span><br><span class="line">                           <span class="type">JSONArray</span> <span class="variable">jsonArray</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JSONArray</span>(responseData);</span><br><span class="line">                           <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>;i &lt; jsonArray.length();i++) &#123;</span><br><span class="line">                               <span class="type">JSONObject</span> <span class="variable">jsonObject</span> <span class="operator">=</span> jsonArray.getJSONObject(i);</span><br><span class="line">                               Log.d(TAG, <span class="string">&quot;run: userId = &quot;</span>+jsonObject.getInt(<span class="string">&quot;userId&quot;</span>));</span><br><span class="line">                               Log.d(TAG, <span class="string">&quot;run: userName = &quot;</span>+jsonObject.getString(<span class="string">&quot;userName&quot;</span>));</span><br><span class="line">                               Log.d(TAG, <span class="string">&quot;run: userAddress = &quot;</span>+jsonObject.getString(<span class="string">&quot;userAddress&quot;</span>));</span><br><span class="line">                           &#125;</span><br><span class="line">                           runOnUiThread(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                               <span class="meta">@Override</span></span><br><span class="line">                               <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                                   Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;Query Success&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                               &#125;</span><br><span class="line">                           &#125;);</span><br><span class="line">                       &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                           e.printStackTrace();</span><br><span class="line">                           runOnUiThread(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                               <span class="meta">@Override</span></span><br><span class="line">                               <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                                   Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;Query Error&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                               &#125;</span><br><span class="line">                           &#125;);</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;).start();</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;);</span><br></pre></td></tr></table></figure>

<h3 id="File类型-1"><a href="#File类型-1" class="headerlink" title="File类型"></a>File类型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> <span class="type">int</span> <span class="variable">WRITE_EXTERNAL_STORAGE_REQUEST_CODE</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">onCreate</span><span class="params">(Bundle savedInstanceState)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//检查存储权限</span></span><br><span class="line">        <span class="keyword">if</span>(ContextCompat.checkSelfPermission(<span class="built_in">this</span>, Manifest.permission.WRITE_EXTERNAL_STORAGE)</span><br><span class="line">        != PackageManager.PERMISSION_GRANTED) &#123;<span class="comment">//没有权限，向用户请求权限</span></span><br><span class="line">            ActivityCompat.requestPermissions(MainActivity.<span class="built_in">this</span>,</span><br><span class="line">                    <span class="keyword">new</span> <span class="title class_">String</span>[] &#123;</span><br><span class="line">                            Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE</span><br><span class="line">                    &#125;,WRITE_EXTERNAL_STORAGE_REQUEST_CODE);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="type">Button</span> <span class="variable">upload</span> <span class="operator">=</span> findViewById(R.id.upload);</span><br><span class="line">        </span><br><span class="line">        upload.setOnClickListener(<span class="keyword">new</span> <span class="title class_">View</span>.OnClickListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">(View view)</span> &#123;</span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            <span class="type">OkHttpClient</span> <span class="variable">client</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">OkHttpClient</span>();</span><br><span class="line">                            <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;/mnt/sdcard/DCIM/Camera/IMG_20221030_074015.jpg&quot;</span>);</span><br><span class="line">                            MultipartBody.<span class="type">Builder</span> <span class="variable">requestBody</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MultipartBody</span>.Builder()</span><br><span class="line">                                    .setType(MultipartBody.FORM);<span class="comment">//通过表单上传文件</span></span><br><span class="line">                            <span class="type">RequestBody</span> <span class="variable">fileBody</span> <span class="operator">=</span> RequestBody.create(MediaType.parse(<span class="string">&quot;image/*&quot;</span>),file);<span class="comment">//上传的文件及类型</span></span><br><span class="line">                            requestBody.addFormDataPart(<span class="string">&quot;file&quot;</span>,file.getName(),fileBody);<span class="comment">//参数：请求key，文件名称，filebody</span></span><br><span class="line">                            <span class="type">Request</span> <span class="variable">request</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Request</span>.Builder()</span><br><span class="line">                                    .url(<span class="string">&quot;http://172.17.152.37:8080/user/upload&quot;</span>)</span><br><span class="line">                                    .post(requestBody.build())</span><br><span class="line">                                    .build();</span><br><span class="line">                            client.newBuilder().readTimeout(<span class="number">5000</span>, TimeUnit.MILLISECONDS)</span><br><span class="line">                                    .build().newCall(request).enqueue(<span class="keyword">new</span> <span class="title class_">Callback</span>() &#123;</span><br><span class="line">                                        <span class="meta">@Override</span></span><br><span class="line">                                        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onFailure</span><span class="params">(<span class="meta">@NonNull</span> Call call, <span class="meta">@NonNull</span> IOException e)</span> &#123;</span><br><span class="line">                                            Log.d(TAG, <span class="string">&quot;onFailure: 文件上传失败&quot;</span>);</span><br><span class="line">                                            e.printStackTrace();</span><br><span class="line">                                        &#125;</span><br><span class="line"></span><br><span class="line">                                        <span class="meta">@Override</span></span><br><span class="line">                                        <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onResponse</span><span class="params">(<span class="meta">@NonNull</span> Call call, <span class="meta">@NonNull</span> Response response)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">                                            <span class="keyword">if</span> (response.isSuccessful()) &#123;</span><br><span class="line">                                                <span class="keyword">try</span> &#123;</span><br><span class="line">                                                    <span class="type">JSONObject</span> <span class="variable">jsonObject</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JSONObject</span>(response.body().string());</span><br><span class="line">                                                    Log.d(TAG,</span><br><span class="line">                                                            <span class="string">&quot;onResponse: 文件上传成功&quot;</span>+jsonObject.getString(<span class="string">&quot;code&quot;</span>));</span><br><span class="line">                                                    Log.d(TAG,</span><br><span class="line">                                                            <span class="string">&quot;onResponse: 服务器上的文件名&quot;</span>+jsonObject.getString(<span class="string">&quot;body&quot;</span>));</span><br><span class="line">                                                &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                                                    e.printStackTrace();</span><br><span class="line">                                                &#125;</span><br><span class="line">                                            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                                                Log.d(TAG, <span class="string">&quot;onResponse: 文件+&quot;</span>+response.message()+</span><br><span class="line">                                                        <span class="string">&quot;error:body&quot;</span>+response.body().string());</span><br><span class="line">                                            &#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                    &#125;);</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                            e.printStackTrace();</span><br><span class="line">                            runOnUiThread(<span class="keyword">new</span> <span class="title class_">Runnable</span>() &#123;</span><br><span class="line">                                <span class="meta">@Override</span></span><br><span class="line">                                <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">                                    Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;UpLoad Error&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).start();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onRequestPermissionsResult</span><span class="params">(<span class="type">int</span> requestCode, <span class="meta">@NonNull</span> String[] permissions, <span class="meta">@NonNull</span> <span class="type">int</span>[] grantResults)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>.onRequestPermissionsResult(requestCode, permissions, grantResults);</span><br><span class="line">        <span class="keyword">if</span>(requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>;i &lt; permissions.length;i++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(grantResults[i] != PackageManager.PERMISSION_GRANTED)<span class="comment">//用户选择了禁止</span></span><br><span class="line">                &#123;</span><br><span class="line">                    Toast.makeText(MainActivity.<span class="built_in">this</span>, <span class="string">&quot;权限获取失败&quot;</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">                    finish();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/xlrblog/tags/Android/" rel="tag"># Android</a>
              <a href="/xlrblog/tags/Note/" rel="tag"># Note</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/xlrblog/2022/10/26/Android%E4%BD%BF%E7%94%A8%E7%BD%91%E7%BB%9C%E6%8A%80%E6%9C%AF/" rel="prev" title="Android使用网络技术">
      <i class="fa fa-chevron-left"></i> Android使用网络技术
    </a></div>
      <div class="post-nav-item">
    <a href="/xlrblog/2022/11/09/Android%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%BC%96%E7%A8%8B%E4%B8%8E%E6%9C%8D%E5%8A%A1/" rel="next" title="Android多线程编程与服务">
      Android多线程编程与服务 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95"><span class="nav-number">1.</span> <span class="nav-text">接口测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Body%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.1.</span> <span class="nav-text">Body类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Param%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.2.</span> <span class="nav-text">Param类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#File%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.3.</span> <span class="nav-text">File类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E5%90%8E%E7%AB%AF%E8%BF%9E%E6%8E%A5"><span class="nav-number">2.</span> <span class="nav-text">前后端连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Body%E7%B1%BB%E5%9E%8B-1"><span class="nav-number">2.1.</span> <span class="nav-text">Body类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Param%E7%B1%BB%E5%9E%8B-1"><span class="nav-number">2.2.</span> <span class="nav-text">Param类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#File%E7%B1%BB%E5%9E%8B-1"><span class="nav-number">2.3.</span> <span class="nav-text">File类型</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="君不见"
      src="/xlrblog/images/avatar.gif">
  <p class="site-author-name" itemprop="name">君不见</p>
  <div class="site-description" itemprop="description">君不见，黄河之水天上来，奔流到海不复回。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/xlrblog/archives/">
        
          <span class="site-state-item-count">50</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">25</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/decxlr" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;decxlr" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">君不见</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/xlrblog/lib/anime.min.js"></script>
  <script src="/xlrblog/lib/velocity/velocity.min.js"></script>
  <script src="/xlrblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/xlrblog/js/utils.js"></script>

<script src="/xlrblog/js/motion.js"></script>


<script src="/xlrblog/js/schemes/pisces.js"></script>


<script src="/xlrblog/js/next-boot.js"></script>




  




  
<script src="/xlrblog/js/local-search.js"></script>













  

  

</body>
</html>
